package com.codetype.config.security.detailservice;

import com.codetype.config.security.exception.UserException;
import com.codetype.web.blog.entity.User;
import com.codetype.web.blog.service.UserService;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.security.PublicKey;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;

/**
 * 自定义数据从数据库中查询
 */
@Component("customerUserDetailsService")
public class CustomerUserDetailsService implements UserDetailsService{

    @Autowired
    private UserService userService;


    @Autowired
    private PasswordEncoder passwordEncoder; //security提供的加密接口

    /**
     * 登录
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username){
        //登录
        User user = userService.getUsersName(username);
        //判断用户是否存在
        if (user==null) throw new InternalAuthenticationServiceException("用户不存在!!!");
        return user;
    }

    public void register(String username,String password){
        User user = new User();
        user.setIsArticle((UUID.randomUUID().toString().replace("-","")));
        user.setUsername(username);
        user.setPassword(password);
        /**
         * 随机头像
         */
        ArrayList<String> list = new ArrayList<>();
        list.add("");
        list.add("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQOr92WYXk5zZr2MSoxGg5jGP-QQCUDTJGnag&usqp=CAU");
        list.add("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcT8LwoEAtiosOkAB1YhQpVA7cwBPNV4g5BNyA&usqp=CAU");
        list.add("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQSyLyJoTwBWH_q93Fitw1YphdP89WahZoUxg&usqp=CAU");
        list.add("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ14WdvWBE05NlsE_oKYWo6YMcpNMWduZVN1w&usqp=CAU");
        list.add("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRWTA8HjQQNit9TG8rk1R92Ou_RnLny5QqwzA&usqp=CAU");
        list.add("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTTa9GAH0Ju_ik4A_RBdpcH2SYTK6_ELW1BiQ&usqp=CAU");
        Random random = new Random();
        int i = random.nextInt(list.size());
        user.setIsImg(list.get(i));
        User usersName = userService.getUsersName(username);
        if (!ObjectUtils.isEmpty(usersName)) throw new UserException("用户名已经存在，注册失败!");
        //加密
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        userService.add(user);
    }
}
